package com.cloud.prac.aop;

import com.cloud.prac.aop.annotation.ReportLog;

import java.lang.reflect.Method;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

/**
 * ReportAop
 *
 * @author 24373
 * @version 1.0
 * @date 2024-07-13 18:15
 */
@Component
@Slf4j
@Aspect
public class ReportAspect {

    @Pointcut("@annotation(com.cloud.prac.aop.annotation.ReportLog)")
    public void pt() {
    }

    private final static ThreadPoolExecutor threadPool = new ThreadPoolExecutor(1, 1, 1, TimeUnit.MINUTES, new LinkedBlockingQueue<>(100));

    @Around("pt()")
    public Object log(ProceedingJoinPoint joinPoint) throws Throwable {
        Object result = joinPoint.proceed();
        threadPool.execute(() -> {
            try {
                MethodSignature signature = (MethodSignature) joinPoint.getSignature();
                Method method = signature.getMethod();
                ReportLog annotation = method.getAnnotation(ReportLog.class);
                String desc = annotation.desc();
                log.info("数据库保存信息{}", desc);
            } catch (Exception e) {
                log.info("数据库保存信息失败{}", e.getMessage());
            }
        });
        return result;
    }

}
